﻿using System.Data;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using YiTu.DBUtility;
using TOUR.SZHOME.Entity;
using tour.szhome.map.DTO;

namespace TOUR.SZHOME.DAL
{
    public partial class ProjectsDAL
    {
        /// <summary>
        /// 获取项目列表 --  前台
        /// </summary>
        /// <param name="XMLB"></param>
        /// <param name="WYLX"></param>
        /// <returns></returns>
        public static List<ProjectsEntity> GetToWeb(string XMLB, string WYLX)
        {
            List<ProjectsEntity> list = new List<ProjectsEntity>();
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XMLB", SqlDbType.NVarChar, 0, ParameterDirection.Input, XMLB));
            string sql = "select * from Projects where 1 = 1 ";
            if (!string.IsNullOrEmpty(XMLB))
            {
                sql += " and XMLB = @XMLB ";
            }
            if (!string.IsNullOrEmpty(WYLX))
            {
                sql += " and WYLX like '%" + WYLX + "%' ";
            }
            
            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, commandParms))
            {
                while (reader.Read())
                {
                    list.Add(ConvertToEntityFromDataReader(reader));
                }
            }

            return list;
        }

        /// <summary>
        /// 为首页下部分获取拼接数据
        /// </summary>
        /// <returns></returns>
        public static List<ProjectDTO> GetToWebIndex(int isTJ, string XMLB)
        {
            List<ProjectDTO> list = new List<ProjectDTO>();

            string sql = @"select Landscape.SYXM,Landscape.JQID,Landscape.JQMC,Landscape.XCLX,Landscape.XGT,Landscape.JQMS,  
                            Projects.XMID,Projects.XMMC,Projects.XMLBT,Projects.XMLB,Projects.XMTS,Projects.XMDZ,Projects.WYLX,Projects.JJ,Projects.TGDZ,Projects.ZXDH 
                            from 
                            Landscape 
                            inner join 
                            Projects
                            on 
                            Landscape.SYXM like '%' + Projects.XMMC + '%'
                             where 1 =1 ";
            if (isTJ == 1)
            {
                sql += " and Landscape.SFTJ = 1";
            }

            if (!string.IsNullOrEmpty(XMLB))
            {
                sql += " and Projects.XMLB = '"+ XMLB +"'";
            }
            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, null))
            {
                while (reader.Read())
                {
                    list.Add(new ProjectDTO()
                    {
                        XMID = Convert.ToInt32(reader["XMID"]),
                        XMMC = reader["XMMC"].ToString(),
                        XMLBT = reader["XMLBT"].ToString(),
                        XMLB = reader["XMLB"].ToString(),
                        XMTS = reader["XMTS"].ToString(),
                        XMDZ = reader["XMDZ"].ToString(),
                        WYLX = reader["WYLX"].ToString(),
                        JJ = reader["JJ"].ToString(),
                        TGDZ = reader["TGDZ"].ToString(),
                        ZXDH = reader["ZXDH"].ToString(),
                        JQID = Convert.ToInt32(reader["JQID"]),
                        XGT = reader["XGT"].ToString(),
                        JQMC = reader["JQMC"].ToString(),
                        XCLX = reader["XCLX"].ToString(),
                        JQMS = reader["JQMS"].ToString()
                    });
                }
            }

            return list;
        }

        /// <summary>
        /// 根据ID获取项目名称
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public static string GetNameByID(int pid) 
        {
            string name = string.Empty;
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XMID", SqlDbType.Int, 0, ParameterDirection.Input, pid));
            string sql = "select XMMC from Projects where XMID = @XMID";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, commandParms))
            {
                if (reader.Read())
                {
                    name = reader["XMMC"].ToString();
                }
            }
            return name;
        }
    }
}
